// NavigateDlg.cpp : implementation file
//

#include "stdafx.h"
#include "../Frame/GYWalkAidApp.h"
#include "NavigateDlg.h"
#include "../Common/GYDataSet.h"
#include "../Common/CommonFun.h"
#include "../PatientMan/PatMan.h"
#include "../PatientMan/SelectWalkAider.h"
#include "../Realtime/RealtimeCollect.h"
#include "../Print/PrintReport.h"
#include "../SysConfig/SystemConfig.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

extern volatile BOOL g_bAppQuit;
static CWnd* s_pInfoLabel = NULL;

/////////////////////////////////////////////////////////////////////////////
// CNavigateDlg dialog


CNavigateDlg::CNavigateDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CNavigateDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CNavigateDlg)
	m_strSerialNo = _T("");
	m_strLoginUser = _T("");
	m_strVersionNo = _T("");
	//}}AFX_DATA_INIT

	CCustomMsgCenter* pMsgCenter = GetApp()->GetCustMsgCenter();
	if(pMsgCenter)
		pMsgCenter->RegCustomMsgProcess(GYWA_MSG_BLUETOOTH_CONNECT_PROGRESS,		this,	(MSG_PROCESS_FUNC)OnConnectProgress);
}

CNavigateDlg::~CNavigateDlg()
{
	CCustomMsgCenter* pMsgCenter = GetApp()->GetCustMsgCenter();
	if(pMsgCenter)
		pMsgCenter->RemoveCustomMsgProcess(GYWA_MSG_BLUETOOTH_CONNECT_PROGRESS,		this,	(MSG_PROCESS_FUNC)OnConnectProgress);
}

void CNavigateDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CNavigateDlg)
	DDX_Text(pDX, IDC_STATIC_SN, m_strSerialNo);
	DDX_Text(pDX, IDC_STATIC_USER, m_strLoginUser);
	DDX_Text(pDX, IDC_STATIC_VERSION_NO, m_strVersionNo);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CNavigateDlg, CDialog)
	//{{AFX_MSG_MAP(CNavigateDlg)
	ON_BN_CLICKED(IDB_PATIENT_MAN, OnPatientMan)
	ON_BN_CLICKED(IDB_QUIT, OnQuit)
	ON_BN_CLICKED(IDC_BUTTON_RT_COLLECT, OnButtonRtCollect)
	ON_BN_CLICKED(IDC_BUTTON_DIAG_REPORT, OnButtonDiagReport)
	ON_BN_CLICKED(IDC_BUTTON_SYS_CONFIG, OnButtonSysConfig)
	ON_BN_CLICKED(IDC_BUTTON_HELP_DOC, OnButtonHelp)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNavigateDlg message handlers

void CNavigateDlg::OnPatientMan() 
{
	CWaitCursor wait;

	if(GetApp()->GetWAMan()->GetWADeviceCount() < 1)
	{
		GYMsgBox(IDS_BT_WA_NOT_FOUND, MB_OK|MB_ICONINFORMATION);
		return;
	}

	CSelectWalkAider dlgSelWA;
	if(dlgSelWA.DoModal() == IDOK)
	{
		CPatMan dlgPatMan;
		dlgPatMan.DoModal();
	}
}

BOOL CNavigateDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();

	s_pInfoLabel = GetDlgItem(IDC_STATIC_PROGRESSINFO);
	if(GetApp()->GetWAMan()->GetWADeviceCount() < 1)
	{
		s_pInfoLabel->SetWindowText(GetResString(IDS_BT_INQUIRE_BTDEV));
	}
	else
	{
		if(GetApp()->GetWAMan()->GetWADeviceCount() < 1)
		{
			s_pInfoLabel->SetWindowText(GetResString(IDS_BT_NOBTDEV_FOUND));
		}
		else
		{
			if(!GetApp()->GetWAMan()->IsAllWAConnected())
				s_pInfoLabel->SetWindowText(GetResString(IDS_BT_INQUIRE_BTDEV));
			else
				s_pInfoLabel->ShowWindow(SW_HIDE);
		}
	}

	ShowWindow(SW_MAXIMIZE);

	m_btnPatMan.LoadBitmaps(IDB_BITMAP_NAVI_PAT_NORMAL, IDB_BITMAP_NAVI_PAT_PRESS, IDB_BITMAP_NAVI_PAT_MO, IDB_BITMAP_NAVI_PAT_NORMAL);
	m_btnPatMan.SubclassDlgItem(IDB_PATIENT_MAN, this);
	m_btnPatMan.SizeToContent();
	
	m_btnRTCollect.LoadBitmaps(IDB_BITMAP_NAVI_RT_NORMAL, IDB_BITMAP_NAVI_RT_PRESS, IDB_BITMAP_NAVI_RT_MO, IDB_BITMAP_NAVI_RT_NORMAL);
	m_btnRTCollect.SubclassDlgItem(IDC_BUTTON_RT_COLLECT, this);
	m_btnRTCollect.SizeToContent();
	
	m_btnDiagReport.LoadBitmaps(IDB_BITMAP_NAVI_REPORT_NORMAL, IDB_BITMAP_NAVI_REPORT_PRESS, IDB_BITMAP_NAVI_REPORT_MO, IDB_BITMAP_NAVI_REPORT_NORMAL);
	m_btnDiagReport.SubclassDlgItem(IDC_BUTTON_DIAG_REPORT, this);
	m_btnDiagReport.SizeToContent();
	
	m_btnSysConfig.LoadBitmaps(IDB_BITMAP_NAVI_SYS_NORMAL, IDB_BITMAP_NAVI_SYS_PRESS, IDB_BITMAP_NAVI_SYS_MO, IDB_BITMAP_NAVI_SYS_NORMAL);
	m_btnSysConfig.SubclassDlgItem(IDC_BUTTON_SYS_CONFIG, this);
	m_btnSysConfig.SizeToContent();
	
	m_btnHelpDoc.LoadBitmaps(IDB_BITMAP_NAVI_HELP_NORMAL, IDB_BITMAP_NAVI_HELP_PRESS, IDB_BITMAP_NAVI_HELP_MO, IDB_BITMAP_NAVI_HELP_NORMAL);
	m_btnHelpDoc.SubclassDlgItem(IDC_BUTTON_HELP_DOC, this);
	m_btnHelpDoc.SizeToContent();
	
	m_btnQuit.LoadBitmaps(IDB_BITMAP_NAVI_CLOSE_NORMAL, IDB_BITMAP_NAVI_CLOSE_PRESS, IDB_BITMAP_NAVI_CLOSE_MO, IDB_BITMAP_NAVI_CLOSE_NORMAL);
	m_btnQuit.SubclassDlgItem(IDB_QUIT, this);
	m_btnQuit.SizeToContent();

	UpdateData(FALSE);

	SetTimer(GYWA_MSG_NAVI_AUTO_BACKUP_DB, 1000 * 20, AutoBackupDbTimerFunc);

	
	GetDlgItem(IDB_PATIENT_MAN)->SetFocus();
	return FALSE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CNavigateDlg::OnOK()
{
	CWnd* pFocus = GetFocus();
	if(pFocus)
	{
		switch(pFocus->GetDlgCtrlID())
		{
		case IDB_PATIENT_MAN:
			OnPatientMan();
			break;

		case IDC_BUTTON_RT_COLLECT:
			OnButtonRtCollect();
			break;
			
		case IDC_BUTTON_DIAG_REPORT:
			OnButtonDiagReport();
			break;
			
		case IDC_BUTTON_SYS_CONFIG:
			OnButtonSysConfig();
			break;
			
		case IDC_BUTTON_HELP_DOC:
			OnButtonHelp();
			break;
			
		case IDB_QUIT:
			OnQuit();
			break;
		}
	}
}

void CNavigateDlg::OnCancel()
{
	OnQuit();
}

void CNavigateDlg::OnQuit() 
{
	if(GYMsgBox(IDS_NAV_CONFIRM_EXIT, MB_YESNO|MB_ICONQUESTION) != IDYES)
		return;

	g_bAppQuit = TRUE;

	GetApp()->GetWAMan()->WaitThreadsQuit();
	EndDialog(IDOK);	
}

void CNavigateDlg::SetLoginUser(CString strLoginUser)
{
	m_strLoginUser = strLoginUser;
}

void CNavigateDlg::SetSerialNo(CString strSN)
{
	m_strSerialNo = strSN;
}

void CNavigateDlg::SetVersionNo(CString strVersionNo)
{
	m_strVersionNo = strVersionNo;
}

void CNavigateDlg::OnConnectProgress(WPARAM wParam, LPARAM lParam)
{
	DWORD dwShowDuration = lParam;
	if(dwShowDuration == 0)
		return;

	CCustomMsgCenter* pMsgCenter = GetApp()->GetCustMsgCenter();
	if(pMsgCenter == NULL)
		return;

	pMsgCenter->StopTimer(GYWA_MSG_NAVI_TIMERID_BT_COMPLETED);

	// 'this' pointer is invalid, use static pointer to reference control in the dialog
	LPCTSTR pMsgInfo = (LPCTSTR)wParam;
	s_pInfoLabel->ShowWindow(SW_SHOW);
	s_pInfoLabel->SetWindowText(pMsgInfo);

	if(dwShowDuration != INFINITE)
	{
		pMsgCenter->RegTimerProcess(GYWA_MSG_NAVI_TIMERID_BT_COMPLETED, this, (MSG_PROCESS_FUNC)OnBTConnectCompleted);
		pMsgCenter->StartTimer(		GYWA_MSG_NAVI_TIMERID_BT_COMPLETED, dwShowDuration);
	}
}

void CNavigateDlg::OnBTConnectCompleted(WPARAM wParam, LPARAM lParam)
{
	CCustomMsgCenter* pMsgCenter = GetApp()->GetCustMsgCenter();
	if(pMsgCenter)
		pMsgCenter->StopTimer(wParam);

	s_pInfoLabel->ShowWindow(SW_HIDE);
}

void CNavigateDlg::OnButtonRtCollect() 
{
	CRealtimeCollect dlg;
	dlg.DoModal();
}

void CNavigateDlg::OnButtonDiagReport() 
{
	CPrintReport dlgPrint;
	dlgPrint.DoModal();
}

void CNavigateDlg::OnButtonSysConfig() 
{
	CSystemConfig dlgSysConfig;
	dlgSysConfig.DoModal();
	UpdateData(FALSE);
}

void CNavigateDlg::OnButtonHelp() 
{
	CString strHelpFile = GetExePath();
	strHelpFile += _T("\\Help\\");
	strHelpFile += GYWA_HELP_INDEX_FILE;

	::ShellExecute(NULL, _T("Open"),  strHelpFile, NULL , NULL, SW_SHOWNORMAL);
}

void CNavigateDlg::AutoBackupDbTimerFunc(HWND hWnd, UINT nMsg, UINT nIDEvent, DWORD dwTime)
{
	CString strIniFile = GetExePath();
	strIniFile += _T("\\");
	strIniFile += GYWA_INI_FILE;

	TCHAR szBackupTime[MAX_PATH] = {0};
	::GetPrivateProfileString(_T("User"), _T("BackupTime"), _T("12:00"), szBackupTime, MAX_PATH, strIniFile);

	//  Check whether current time is the backup time
	CTime tCur = CTime::GetCurrentTime();
	CString strCurHourMinute;
	strCurHourMinute.Format(_T("%02d:%02d"), tCur.GetHour(), tCur.GetMinute());
	if(strCurHourMinute != szBackupTime)
		return;

	//  Check whether already backup
	CString strLastBackupDate = GetConfigValue(_T("LastBackupDate"));
	CString strCurDate;
	strCurDate.Format(_T("%4d%02d%02d"), tCur.GetYear(), tCur.GetMonth(), tCur.GetDay());
	if(strCurDate == strLastBackupDate)
		return;

	CString strDBFileName;
	strDBFileName.Format(_T("%4d-%02d-%02d_%02d%02d%02d"), tCur.GetYear(), tCur.GetMonth(), tCur.GetDay(), tCur.GetHour(), tCur.GetMinute(), tCur.GetSecond());

	//  Read the backup path, and backup to the path.
	
	TCHAR szBackupPath[MAX_PATH] = {0};
	::GetPrivateProfileString(_T("User"), _T("BackupDir"), _T(""), szBackupPath, MAX_PATH, strIniFile);
	
	if( !IsFileExist(szBackupPath) )
	{
		::CreateDirectory( szBackupPath , NULL);
	}
	
	CString strDBFile = szBackupPath;
	strDBFile += _T("\\");
	strDBFile += strDBFileName;
	strDBFile += _T(".db");
	
	TCHAR szCmdExePath[MAX_PATH] = {0};
	if(!GetEnvironmentVariable(_T("ComSpec"), szCmdExePath, MAX_PATH))
	{
		_tcscpy(szCmdExePath, _T("C:\\Windows\\System32\\cmd.exe"));
	}
	
	CString strCmd = szCmdExePath;
	strCmd += _T(" /C ");
	strCmd += GetMySQLDumpCmd();
	strCmd += strDBFile;
	
	PROCESS_INFORMATION	pi = {0};
	STARTUPINFO			si = {0};
	si.cb = sizeof(si);
	
	TCHAR szCmd[MAX_PATH * 2] = {0};
	_tcscpy(szCmd, strCmd);
	BOOL bRet = ::CreateProcess(szCmdExePath, szCmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);

	SetConfigValue(_T("LastBackupDate"), strCurDate);
}
